package com.yocard.yxyd.ui.presenter;

import android.app.Activity;
import android.content.Context;

import com.yocard.yxyd.utils.CommonUtils;
import com.yocard.yxyd.utils.util.AppUtils;
import com.yocard.yxyd.utils.util.CacheDoubleStaticUtils;
import com.yanzhenjie.permission.AndPermission;
import com.yocard.yxyd.base.AppContext;
import com.yocard.yxyd.base.CommonSubscriber;
import com.yocard.yxyd.base.Constants;
import com.yocard.yxyd.base.RxPresenter;
import com.yocard.yxyd.http.HttpManager;
import com.yocard.yxyd.http.model.response.UpdateBean;
import com.yocard.yxyd.ui.contract.MainContract;
import com.yocard.yxyd.ui.dialog.ProgressDialog;
import com.yocard.yxyd.ui.dialog.UpdateDialog;
import com.yocard.yxyd.utils.DownloadUtil;
import com.yocard.yxyd.utils.RxUtil;

import java.io.File;

import javax.inject.Inject;

/**
 * Created on 2019/4/18.
 */
public class MainPresenter extends RxPresenter<MainContract.View> implements MainContract.Presenter {

    protected UpdateDialog updateDialog;

    @Inject
    public MainPresenter(HttpManager manager) {
        super(manager);
    }


    @Override
    public void getUpdateInfo() {
        addSubscribe(httpManager.getUpdateInfo(AppUtils.getAppVersionCode())
                .compose(RxUtil.rxSchedulerHelper())
                .subscribeWith(new CommonSubscriber<UpdateBean>(mView) {
                    @Override
                    public void onSuccess(UpdateBean data) {
                        showUpdateDialog(data);
                    }
                }));
    }

    private void showUpdateDialog(UpdateBean updateBean) {
        UpdateBean.LastVersion lastVersion = updateBean.getLast_version();
        if (updateBean.getIs_new() == 1 && lastVersion != null) {
            boolean isMust = updateBean.getMust() == 1;
            if (updateDialog == null) {
                updateDialog = new UpdateDialog(mView.getContext(), lastVersion.getDescribe(), isMust);
                updateDialog.setUpdateListener(value -> {
                    showProgressDialog(updateBean.getLast_version().getDownload_url());
                });
            }
            if(!updateDialog.isShowing()){
                updateDialog.show();
            }

            if(isMust){
                CacheDoubleStaticUtils.put(Constants.KEY_UPDATE, true);
            }else {
                CacheDoubleStaticUtils.put(Constants.KEY_UPDATE, false);
            }
        }else {
            CacheDoubleStaticUtils.put(Constants.KEY_UPDATE, false);
        }
    }

    private void showProgressDialog(String download_url) {
        Context context = mView.getContext();
        if (context instanceof Activity) {
            Activity activity = (Activity) context;
            ProgressDialog progressDialog = new ProgressDialog(mView.getContext());
            progressDialog.show();
            File cacheDir = mView.getContext().getExternalFilesDir("apk");
            try {
                DownloadUtil.get().download(download_url, cacheDir.getAbsolutePath(), "yxyd.apk", new DownloadUtil.OnDownloadListener() {
                    @Override
                    public void onDownloadSuccess(File file) {
                        progressDialog.dismiss();
                        AndPermission.with(AppContext.getContext())
                                .install()
                                .file(file)
                                .start();
                        CommonUtils.installApk(file,mView.getContext());
                    }

                    @Override
                    public void onDownloading(int progress) {
                        activity.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                progressDialog.setProgress(progress);
                            }
                        });
                    }

                    @Override
                    public void onDownloadFailed(Exception e) {
                        progressDialog.dismiss();
                    }
                });
            } catch (Exception e) {
                progressDialog.dismiss();
            }

        }
    }
}
